package com.fmatos.controller;


import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.net.URL;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.xml.sax.InputSource;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fmatos.controller.directory.AlbumDirectory;
import com.fmatos.freemarker.User;
import com.fmatos.service.album.domain.Album;
import com.fmatos.service.album.downloader.info.AlbumInfo;

@Controller
public class FreemarkerController {
	
    private static List<User> userList = new ArrayList<User>();
    
    //Initialize the list with some data for index screen
    static {
        userList.add(new User("Bill", "Gates"));
        userList.add(new User("Steve", "Jobs"));
        userList.add(new User("Larry", "Page"));
        userList.add(new User("Sergey", "Brin"));
        userList.add(new User("Larry", "Ellison"));
    }
    
	/**
     * Saves the static list of users in model and renders it 
     * via freemarker template.
     * 
     * @param model 
     * @return The index view (FTL)
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(@ModelAttribute("model") ModelMap model) {
 
        model.addAttribute("userList", userList);
 
        return "index";
    }
    
    @RequestMapping(value = "/index/albums", method = RequestMethod.GET)
    public String albums(@ModelAttribute("model") ModelMap model) {
 
    	
        model.addAttribute("albums",AlbumDirectory.getAllAlbums() );
 
        return "albums";
    }

    @RequestMapping(value = "/index/albums/{album}", method = RequestMethod.GET)
    public String albums(@ModelAttribute("model") ModelMap model,String album) {
 
    	if  ( album ==null ) {
    		album = "CustomRss-1482331143"; // pinterest
    	}
    	AlbumInfo info = AlbumDirectory.findAlbumByName(album);
        model.addAttribute("info", info);
        
    	InputStream is = getInputStream(info.getUrl());        
    	if ( is != null ) {
    		ObjectMapper mapper = new ObjectMapper();
    		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    		try {
    			Album photos = mapper.readValue(is, Album.class);
    			
    			model.addAttribute("album", photos);
    			model.addAttribute("photos", photos.getPhotos());
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}

        return "album";
    }

    private InputStream getInputStream(String path) {
    	String host = "http://localhost:8080";

    	path = host + path;
    	URL url;
    	try {
    		url = new URL(path);
    		URLConnection openConnection = url.openConnection();
    		InputStream is = openConnection.getInputStream();

    		if("gzip".equals(openConnection.getContentEncoding())){
    			is = new GZIPInputStream(is);
    		}  
    		
    		return is;
    	} catch (MalformedURLException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	} catch (IOException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}

    	return null;
	}
}
